package com.yanqu.road.dao.impl.activity.jieshi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jieshi.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JieShiUserDataDaoImpl extends TempDataObjectDao<JieShiUserData> {

    public static String getJieShiUserDataSql = "select * from `t_u_activity_jieshi_data` where `activityId`=? and `userId`=?";

    public JieShiUserData getJieShiUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getJieShiUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateJieShiUserDataSql = "update `t_u_activity_jieshi_data` set jieShiLevel=?,hour=?,dollId=?,dollList=?,buffList=?,energy=?,lastEnergyRecTime=?,mapId=?,mapBuildList=?,unlockPosition=?,ruffianList=?,eventList=?,attack=?,jiaoZi=?,score=?,jieDaoSiLevel=?,patronsDataList=?,travelTimes=?,guide=?,nickName=?,changeNameTimes=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(JieShiUserData jieShiUserData) {
        jieShiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getJieShiLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getHour()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getDollId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getDollList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getBuffList())));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getMapId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getMapBuildList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getUnlockPosition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getRuffianList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getEventList())));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getAttack()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getJiaoZi()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getJieDaoSiLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getPatronsDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getTravelTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, jieShiUserData.isGuide()));
        para.put(i++, new DbParameter(Types.VARCHAR, jieShiUserData.getNickName()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getChangeNameTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getUserId()));
        if (getDb().executeNoneQuery(updateJieShiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertJieShiUserDataSql = "insert into `t_u_activity_jieshi_data`(activityId,userId,jieShiLevel,hour,dollId,dollList,buffList,energy,lastEnergyRecTime,mapId,mapBuildList,unlockPosition,ruffianList,eventList,attack,jiaoZi,score,jieDaoSiLevel,patronsDataList,travelTimes,guide,nickName,changeNameTimes) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(JieShiUserData jieShiUserData) {
        jieShiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getJieShiLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getHour()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getDollId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getDollList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getBuffList())));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getMapId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getMapBuildList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getUnlockPosition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getRuffianList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getEventList())));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getAttack()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getJiaoZi()));
        para.put(i++, new DbParameter(Types.BIGINT, jieShiUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getJieDaoSiLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jieShiUserData.getPatronsDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getTravelTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, jieShiUserData.isGuide()));
        para.put(i++, new DbParameter(Types.VARCHAR, jieShiUserData.getNickName()));
        para.put(i++, new DbParameter(Types.INTEGER, jieShiUserData.getChangeNameTimes()));
        if (getDb().executeNoneQuery(insertJieShiUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected JieShiUserData parseData(ResultSet rs) throws SQLException {
        JieShiUserData jieShiUserData = new JieShiUserData();
        jieShiUserData.setActivityId(rs.getInt("activityId"));
        jieShiUserData.setUserId(rs.getLong("userId"));
        jieShiUserData.setJieShiLevel(rs.getInt("jieShiLevel"));
        jieShiUserData.setHour(rs.getInt("hour"));
        jieShiUserData.setDollId(rs.getInt("dollId"));
        jieShiUserData.setDollList(JSON.parseObject(rs.getString("dollList"), new TypeReference<List<JieShiDollData>>() {
        }));
        String buffList = rs.getString("buffList");
        if (!StringUtils.isNullOrEmpty(buffList)) {
            jieShiUserData.setBuffList(JSON.parseObject(buffList, new TypeReference<List<JieShiBuffData>>() {
            }));
        }
        jieShiUserData.setEnergy(rs.getInt("energy"));
        jieShiUserData.setLastEnergyRecTime(rs.getLong("lastEnergyRecTime"));
        jieShiUserData.setMapId(rs.getInt("mapId"));
        String mapBuildList = rs.getString("mapBuildList");
        if (!StringUtils.isNullOrEmpty(mapBuildList)) {
            jieShiUserData.setMapBuildList(JSON.parseObject(mapBuildList, new TypeReference<List<JieShiMapBuildData>>() {
            }));
        }
        String unlockPosition = rs.getString("unlockPosition");
        if (!StringUtils.isNullOrEmpty(unlockPosition)) {
            jieShiUserData.setUnlockPosition(JSON.parseObject(unlockPosition, new TypeReference<List<Integer>>() {
            }));
        }
        String ruffianList = rs.getString("ruffianList");
        if (!StringUtils.isNullOrEmpty(ruffianList)) {
            jieShiUserData.setRuffianList(JSON.parseObject(ruffianList, new TypeReference<List<JieShiRuffianData>>() {
            }));
        }
        String eventList = rs.getString("eventList");
        if (!StringUtils.isNullOrEmpty(eventList)) {
            jieShiUserData.setEventList(JSON.parseObject(eventList, new TypeReference<List<JieShiEventData>>() {
            }));
        }
        jieShiUserData.setAttack(rs.getLong("attack"));
        jieShiUserData.setJiaoZi(rs.getLong("jiaoZi"));
        jieShiUserData.setScore(rs.getLong("score"));
        jieShiUserData.setJieDaoSiLevel(rs.getInt("jieDaoSiLevel"));
        String patronsDataList = rs.getString("patronsDataList");
        if (!StringUtils.isNullOrEmpty(patronsDataList)) {
            jieShiUserData.setPatronsDataList(JSON.parseObject(patronsDataList, new TypeReference<List<JieShiPatronsData>>() {
            }));
        }
        jieShiUserData.setTravelTimes(rs.getInt("travelTimes"));
        jieShiUserData.setGuide(rs.getBoolean("guide"));
        jieShiUserData.setNickName(rs.getString("nickName"));
        jieShiUserData.setChangeNameTimes(rs.getInt("changeNameTimes"));
        return jieShiUserData;
    }

}
